package NetworkConfiguration;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class NSetExampleMessage_ResourceYS extends Composite {

	private Table table1;

	private Table table2;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public NSetExampleMessage_ResourceYS(Composite parent, int style) {
		super(parent, style);
		setSize(1000, 800);
		setLayout(new GridLayout(3,true));
		new Label(this, SWT.NONE);
		
		Label label = new Label(this, SWT.NONE);//标题
		label.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false, 1, 1));
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		//label.setBounds(10, 10, 180, 24);
		label.setText("网络配置-资源约束");
		new Label(this, SWT.NONE);
		
		Label label_1 = new Label(this, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		//label_1.setBounds(10, 55, 331, 24);
		label_1.setText("请在下表中配置交换机所需资源条件：");
		new Label(this, SWT.NONE);
		new Label(this, SWT.NONE);
		
		// Table1的初始化
		table1 = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		table1.setHeaderVisible(true);
		table1.setLinesVisible(true);
		//table1.setItemCount(ITEM_COUNT);
		//table1.setBounds(10, 10, 400, 300);
		table1.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,3,1));
		
		
		//设置表1纵坐标
				/*TableItem[] items = new TableItem[ITEM_COUNT];
				//for (int i = 0; i < ITEM_COUNT; i++) {
				//items[i] = new TableItem(table1, SWT.CENTER);
					//columns[i].setText("Column "+new Integer(i+1).toString());
					//columns[i].setWidth(COLUMN_WIDTH);
				}*/
		
		
		TableItem tableItem_1 = new TableItem(table1, SWT.NONE);
		tableItem_1.setText("信号1");
				
		TableItem tableItem_2 = new TableItem(table1, SWT.NONE);
		tableItem_2.setText("信号2");
				
		TableItem tableItem_3 = new TableItem(table1, SWT.NONE);
		tableItem_3.setText("信号3");
				
		TableItem tableItem_4 = new TableItem(table1, SWT.NONE);
		tableItem_4.setText(new String[] {});
		tableItem_4.setText("信号4");
		
		
		
				/*设置表1横坐标
		TableColumn[] columns = new TableColumn[COLUMN_COUNT];
		for (int i = 0; i < COLUMN_COUNT; i++) {
			columns[i] = new TableColumn(table1, SWT.CENTER);
		
		 }*/
		
		TableColumn tableColumn_1 = new TableColumn(table1, SWT.NONE);
		tableColumn_1.setWidth(100);
		tableColumn_1.setText("");
		
		TableColumn tableColumn_2 = new TableColumn(table1, SWT.NONE);
		tableColumn_2.setWidth(100);
		tableColumn_2.setText("信号描述");
		
		TableColumn tableColumn_3 = new TableColumn(table1, SWT.NONE);
		tableColumn_3.setWidth(106);
		tableColumn_3.setText("信号类型");
		
		TableColumn tableColumn_4 = new TableColumn(table1, SWT.NONE);
		tableColumn_4.setWidth(100);
		tableColumn_4.setText("资源1");
		
		TableColumn tableColumn_5 = new TableColumn(table1, SWT.NONE);
		tableColumn_5.setWidth(100);
		tableColumn_5.setText("资源2");
		
		TableColumn tableColumn_6 = new TableColumn(table1, SWT.NONE);
		tableColumn_6.setWidth(100);
		tableColumn_6.setText("资源3");
		
		
		//
		Label label_2 = new Label(this, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,true,false,3,1));
		//label_2.setBounds(10, 269, 321, 24);
		label_2.setText("请在下表中配置交换机资源条件：");
		
		
		// Table2的初始化
		table2 = new Table(this, SWT.BORDER	| SWT.FULL_SELECTION);
		table2.setHeaderVisible(true);
		table2.setLinesVisible(true);
		table2.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,3,1));
				
				
		TableItem tableItem1 = new TableItem(table2, SWT.NONE);
		tableItem1.setText("交换机1");
				
		TableItem tableItem2 = new TableItem(table2, SWT.NONE);
		tableItem2.setText("交换机2");
				
		TableItem tableItem3 = new TableItem(table2, SWT.NONE);
		tableItem3.setText("交换机3");
				
		TableItem tableItem4 = new TableItem(table2, SWT.NONE);
		tableItem4.setText(new String[] {});
		tableItem4.setText("交换机4");
		
		TableColumn tableColumn1 = new TableColumn(table2, SWT.NONE);
		tableColumn1.setWidth(100);
		tableColumn1.setText("");
		
		TableColumn tableColumn_21 = new TableColumn(table2, SWT.NONE);
		tableColumn_21.setWidth(100);
		tableColumn_21.setText("交换机描述");
		
		TableColumn tableColumn_31 = new TableColumn(table2, SWT.NONE);
		tableColumn_31.setWidth(106);
		tableColumn_31.setText("交换机类型");
				
		TableColumn tableColumn2 = new TableColumn(table2, SWT.NONE);
		tableColumn2.setWidth(100);
		tableColumn2.setText("资源1");
				
		TableColumn tableColumn3 = new TableColumn(table2, SWT.NONE);
		tableColumn3.setWidth(100);
		tableColumn3.setText("资源2");
				
		TableColumn tableColumn4 = new TableColumn(table2, SWT.NONE);
		tableColumn4.setWidth(100);
		tableColumn4.setText("资源3");
			     
		TableColumn tableColumn5 = new TableColumn(table2, SWT.NONE);
		tableColumn5.setWidth(100);
		tableColumn5.setText("...");
				
				
		//设置按钮
		Button button_1 = new Button(this, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		button_1.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, 1, 1));
		button_1.setText("返回");
		
				
		Button button_2 = new Button(this, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		button_2.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, false, false, 1, 1));
		button_2.setText("导出为文件");
				
						
				
		Button button_3 = new Button(this, SWT.NONE);
		button_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		button_3.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, false, false, 1, 1));
		button_3.setText("保存");
		
		table1.addListener(SWT.MouseDoubleClick, listener);
		table2.addListener(SWT.MouseDoubleClick, listener1);

	}
	
	//为每个TableItem增加Listener， 当有Selection事件时， 根据行和列可以定位到每一个Cell，然后就可以处理事件，
	//为一个Cell添加可编辑的方法是增加一个TableEditor
	private Listener listener = new Listener() {
		private int EDITABLECOLUMN;

		public void handleEvent(Event event) {
			//获取鼠标点击的位置
			Point point = new Point(event.x, event.y);
		
			//得到鼠标点击的行（item）
			TableItem item = table1.getItem(point);
			if (item == null) {
				return;
			}
			for (int i = 1; i < 6; i++) {
				
				//得到选中行（item）的每一列的矩形范围，根据鼠标点击位置point来进一步确定是哪一格（cell）
				Rectangle rect = item.getBounds(i);
				if (rect.contains(point)) {
					EDITABLECOLUMN = i;
					
					final TableEditor editor = new TableEditor(table1);
					Control oldEditor = editor.getEditor();
					if (oldEditor != null)
					{
						oldEditor.dispose();
					}
					
					// 添加Text，并使高度符合item的高度
					final Text text = new Text(table1, SWT.NONE);
					text.computeSize(SWT.DEFAULT,table1.getItemHeight());

					//调整editor的高度宽度
					editor.grabHorizontal = true;
					editor.minimumHeight = text.getSize().y;
					editor.minimumWidth = text.getSize().x;
					
					editor.setEditor(text, item, EDITABLECOLUMN);
					
					text.setText(item.getText(EDITABLECOLUMN));

					text.forceFocus();
					
					// 开始编辑的事件
					text.addModifyListener(new ModifyListener() {
						public void modifyText(ModifyEvent e) {
							editor.getItem().setText(EDITABLECOLUMN,text.getText());
						}
					});
					
					//编辑后内容恢复表格前景色
					text.addFocusListener(new FocusAdapter() {
						public void focusLost(FocusEvent e) {
						}
					});
				}
			}
		}
	};
	
	private Listener listener1 = new Listener() {
		private int EDITABLECOLUMN;

		public void handleEvent(Event event) {
			//获取鼠标点击的位置
			Point point = new Point(event.x, event.y);
		
			//得到鼠标点击的行（item）
			TableItem item = table2.getItem(point);
			if (item == null) {
				return;
			}
			for (int i = 1; i < 6; i++) {
				
				//得到选中行（item）的每一列的矩形范围，根据鼠标点击位置point来进一步确定是哪一格（cell）
				Rectangle rect = item.getBounds(i);
				if (rect.contains(point)) {
					EDITABLECOLUMN = i;
					
					final TableEditor editor = new TableEditor(table2);
					Control oldEditor = editor.getEditor();
					if (oldEditor != null)
					{
						oldEditor.dispose();
					}
					
					// 添加Text，并使高度符合item的高度
					final Text text = new Text(table2, SWT.NONE);
					text.computeSize(SWT.DEFAULT,table2.getItemHeight());

					//调整editor的高度宽度
					editor.grabHorizontal = true;
					editor.minimumHeight = text.getSize().y;
					editor.minimumWidth = text.getSize().x;
					
					editor.setEditor(text, item, EDITABLECOLUMN);
					
					text.setText(item.getText(EDITABLECOLUMN));

					text.forceFocus();
					
					// 开始编辑的事件
					text.addModifyListener(new ModifyListener() {
						public void modifyText(ModifyEvent e) {
							editor.getItem().setText(EDITABLECOLUMN,text.getText());
						}
					});
					
					//编辑后内容恢复表格前景色
					text.addFocusListener(new FocusAdapter() {
						public void focusLost(FocusEvent e) {
						}
					});
				}
			}
		}
	};
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
